#!/bin/bash
### Build glog
### Build gtest

export BUILD_LOG_FILE=build.log

function if_error
{
    if [ $1 -ne 0 ]
    then 
        echo "...........................................failed!error code:$1" >> $BUILD_LOG_FILE
        exit $1
    else
        echo "...........................................success!" >> $BUILD_LOG_FILE
    fi
}

function install
{
	name=$1
	unzip="tar zxvf ${name}.tar.gz"
	echo "start build ${name}" >> $BUILD_LOG_FILE
	if [ ! -f ${name}.tar.gz ]; then
		if [ ! -f ${name}.zip ]; then
			if [ ! -f ${name}.tar.bz2 ]; then
				echo ".....Error: Install packet ${name}.tar.gz or ${name}.zip or ${name}.tar.bz2 not been found in $(pwd)"
				exit -1
			else
				unzip="tar jxvf ${name}.tar.bz2"
			fi
		else
			unzip="unzip ${name}.zip"
		fi
	fi
	rm -rf ${name}
	${unzip}
	cd ${name}
	echo "    configure ${name}" >> $BUILD_LOG_FILE
	./configure $2
	if_error $?
	echo "    make ${name}" >> $BUILD_LOG_FILE
	make
	if_error $?
	echo "    make install ${name}" >> $BUILD_LOG_FILE
	make install
	if_error $?
	cd ../
#	rm -rf ${name}
}

function install_gtest
{
	echo "Start build gtest-1.6.0" >> $BUILD_LOG_FILE
	unzip ./gtest-1.6.0.zip
	cd ./gtest-1.6.0
	echo "    configure gtest-1.6.0" >> $BUILD_LOG_FILE
	./configure
	if_error $?
	echo "    make gtest-1.6.0" >> $BUILD_LOG_FILE
	make
	if_error $?
	cp -d ./lib/.libs/*.so* ../local/lib/
	cp ./lib/*.la ../local/lib/
	cp ./lib/.libs/*.a ../local/lib/
	cp ./lib/.libs/*.lai ../local/lib/
	mkdir -p ../local/include/gtest
	cp -R ./include/gtest/* ../local/include/gtest/
	cd ../
	rm -rf ./gtest-1.6.0
}

function install_tinyxml
{
        echo "Start build tinyxml" >> $BUILD_LOG_FILE
        unzip ./tinyxml.zip
        cd ./tinyxml
        echo "    make tinyxml" >> $BUILD_LOG_FILE
        make
        if_error $?
	echo "    make tinyxml.a" >> $BUILD_LOG_FILE
	ar crv libtinyxml.a *.o
	if_error $?
	cp ./libtinyxml.a ../local/lib/
	mkdir -p ../local/include/tinyxml
	cp ./*.h ../local/include/tinyxml/
	cd ..
	rm -rf ./tinyxml
}

function install_libevhtp
{
	echo "Start build libevhtp" >> $BUILD_LOG_FILE
	rm ./libevhtp -rf
	tar zxvf ./libevhtp.tar.gz
	cd ./libevhtp
	echo "    cmake" >> $BUILD_LOG_FILE
	cd build
	PATH=${ZSYSTEM_PATH}/opensource/local/lib:$PATH cmake -DEVHTP_DISABLE_SSL:STRING=ON -DEVHTP_DIABLE_REGEX:STRING=ON ..
	if_error $?
	echo "    make" >> $BUILD_LOG_FILE
	PATH=${ZSYSTEM_PATH}/opensource/local/lib:$PATH make
	if_error $?
	cp ./libevhtp.a ../../local/lib/
	cd ..
	cp ./evhtp.h ../local/include/
	cp ./evthr/evthr.h ../local/include/
	cp ./htparse/htparse.h ../local/include/
}

function install_zzjson
{
	echo "Start build zzjson" >> $BUILD_LOG_FILE
	rm ./zzjson -rf
	tar zxvf ./zzjson.tar.gz
	cd ./zzjson
	echo "    build" >> $BUILD_LOG_FILE
	./buildstatic.sh
	if_error $?
	cp ./lib/* ../local/lib/
	cp ./include/* ../local/include/
	cp ./unicode/*.h ../local/include/
	rm ./zzjson -rf
}

function install_rapidjson
{
        echo "Start install rapidjson" >> $BUILD_LOG_FILE
        rm ./rapidjson -rf
        tar zxvf ./rapidjson.tar.gz
        cp -r ./rapidjson ./local/include/
        rm ./rapidjson -rf
}

function install_hiredis
{
        echo "Start install redis" >> $BUILD_LOG_FILE
        rm ./hiredis-master -rf
        unzip ./hiredis.zip
	cd ./hiredis-master
	make
	if_error $?
	cp ./libhiredis.a ../local/lib/
        cp ./hiredis.h ../local/include/
	cd ..
}

mkdir -p ./local/lib
mkdir -p ./local/include

install_gtest
install_tinyxml
install_rapidjson
install_hiredis

